//
//  UIImage+ZGWImageWithName.m
//  CoreAnimation
//
//  Created by 张国文 on 16/12/29.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "UIImage+ZGWImageWithName.h"
#import "CoreAnimaitonHandle.h"

#define kBundleName @"picture"

@implementation UIImage (ZGWImageWithName)

+ (UIImage *)imageWithName:(NSString *)string
{
    // 分割字符串
    NSArray *pictureArray = [string componentsSeparatedByString:@"."];
    // 图片的名字
    NSString *pictureName = [pictureArray firstObject];
    // 图片的类型
    NSString *pictureCategary = [pictureArray lastObject];
    if (pictureArray.count == 1) {
        // 说明是png图片
        pictureName = [pictureArray firstObject];
        pictureCategary = @"png";
    } else {
        pictureName = [pictureArray firstObject];
        // 图片的类型
        pictureCategary = [pictureArray lastObject];
    }
    
  //  NSLog(@"图片的名字：%@，图片的类型:%@", pictureName, pictureCategary);
    
    return [UIImage imageWithContentsOfFile:[self picturePathWithName:pictureName andCategory:pictureCategary]];
}

// 图片的path，文件夹的名字必须是英文，不能含有中文，目前我并没有解决中文文件夹的显示
+ (NSString *)picturePathWithName:(NSString *)name andCategory:(NSString *)category
{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:kBundleName ofType:@".bundle"];
  //  NSLog(@"目录：%@", [manager subpathsOfDirectoryAtPath:bundlePath error:nil]);

  __block  NSString *resultName = @"";
   __block NSString *resultCategory = @"";
   __block  NSString *resultDirectory = @"";
    NSArray *directoryArray = [manager subpathsOfDirectoryAtPath:bundlePath error:nil];
    [directoryArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 目录
       
        // 首先判断是否是字符串
        if ([obj isKindOfClass:[NSString class]]) {
            // 首先根据"/"分割字符串
            NSString *directoryString = (NSString *)obj;
            NSArray *directoryArray = [directoryString componentsSeparatedByString:@"/"];
            if (directoryArray.count == 1) {
                resultDirectory = @"";
                // 说明是在第一层，根据"."分个字符串
                NSArray *filepathArray = [directoryString componentsSeparatedByString:@"."];
                NSString *fileName = [filepathArray firstObject];
                NSString *categoryName = [filepathArray lastObject];
                if ([fileName isEqualToString:name] && [categoryName isEqualToString:category]) {
                    resultName = fileName;
                    resultCategory = categoryName;
                // 跳出遍历
                    *stop = YES;
                }
            } else {
                // 说明在二层已经二层以后
                // 获取获取路径
               resultDirectory = [directoryString substringToIndex:(directoryString.length - 1 - [[directoryArray lastObject] length])];
                // 根据"."分个字符串
                NSArray *filepathArray = [[directoryArray lastObject] componentsSeparatedByString:@"."];
                NSString *fileName = [filepathArray firstObject];
                NSString *categoryName = [filepathArray lastObject];
                if ([fileName isEqualToString:name] && [categoryName isEqualToString:category]) {
                    resultName = fileName;
                    resultCategory = categoryName;
                    // 跳出遍历
                    *stop = YES;
                }
            }
        }
    }];
    NSLog(@"目录：%@，名字：%@，种类：%@", resultDirectory, resultName, resultCategory);

    NSBundle *pictureBundle = [NSBundle bundleWithPath:bundlePath];
    return [pictureBundle pathForResource:resultName ofType:resultCategory inDirectory:[resultDirectory stringByReplacingPercentEscapesUsingEncoding:NSUnicodeStringEncoding]];
}

@end
